package com.jason.bookworm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jason.bookworm.dao.mapper.CommentMapper;
import com.jason.bookworm.dao.pojo.Book;
import com.jason.bookworm.dao.pojo.Comment;
import com.jason.bookworm.dao.pojo.User;
import com.jason.bookworm.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Override
    public List<Comment> getAllCommOfBook(Book book) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("c_bookid", book.getId())
                .eq("c_status", "0");
        return commentMapper.selectList(queryWrapper);
    }

    @Override
    public List<Comment> getCommOfBookByPages(int pages, Book book) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("c_bookid", book.getId())
                .eq("c_status", "0");
        Page<Comment> comments = commentMapper.selectPage(new Page<>(pages, 10), queryWrapper);
        return comments.getRecords();
    }

    @Override
    public Integer getCommNumOfBook(Book book) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("c_bookid", book.getId())
                .eq("c_status", "0");
        return Math.toIntExact(commentMapper.selectCount(queryWrapper));
    }

    @Override
    public Integer getCalCommNum(Book book) {
        return getCommNumOfBook(book) / 20;
    }

    @Override
    public void deleteOneComm(User user, Comment comment) {
        comment.setStatus("1");
        commentMapper.updateById(comment);
    }
}
